package com.gagakuai.bpm.common.component.aop;

import com.gagakuai.bpm.domian.events.CallBusinessTaskCompleteEvent;
import com.gagakuai.bpm.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;


@Aspect
@Slf4j
@Component
public class CallBusinessInterfaceAop implements ApplicationEventPublisherAware {

    private ApplicationEventPublisher applicationEventPublisher;

    @AfterReturning(value = "@annotation(com.gagakuai.bpm.common.annotation.MyAspect)", returning="target")
    public void callBusinessAop(JoinPoint joinPoint, Object target){
        Object[] objects = joinPoint.getArgs();
        if(objects.length <= 0){
            return;
        }

        if(ObjectUtils.isEmpty(target)){
            if ("completeTask".equals(joinPoint.getSignature().getName())) {
                applicationEventPublisher.publishEvent(new CallBusinessTaskCompleteEvent(applicationEventPublisher, objects[0].toString(), UserUtil.get().getToken()));
            }
        }
    }

    @Override
    public void setApplicationEventPublisher(@NonNull ApplicationEventPublisher applicationEventPublisher) {
        this.applicationEventPublisher = applicationEventPublisher;
    }
}
